/**
 * 父进程通过管道传输两个数据给子进程
 * 由子进程负责从管道中读取并输出
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{

    int pipe_fds[2];
    if (pipe(pipe_fds) < 0)
    {
        perror("pipe error: ");
        exit(EXIT_FAILURE);
    }

    int pid = fork();
    if (0 > pid)
    {
        perror("fork error: ");
        exit(EXIT_FAILURE);
    }
    else if (0 == pid) // 子进程
    {
        close(pipe_fds[1]);

        int value1, value2;
        read(pipe_fds[0], &value1, sizeof(int));
        read(pipe_fds[0], &value2, sizeof(int));

        close(pipe_fds[0]);

        printf("\nvalue1 = %d\tvalue2 = %d\n", value1, value2);
    }
    else // 父进程
    {
        close(pipe_fds[0]);

        int value1 = 1, value2 = 100;
        write(pipe_fds[1], &value1, sizeof(int));
        write(pipe_fds[1], &value2, sizeof(int));

        close(pipe_fds[1]);
    }

    return 0;
}